<?php
namespace Common\Model;
use Common\Model\BaseModel;
/**
 * 权限规则model
 */
class SchoolinfoModel extends BaseModel{

    //添加数据
    public function addData(){
        // 获取post数据
        $data=I('post.');
//        print_r($data);exit;
        // 反转义为下文的 preg_replace使用
        for($i=0; $i<count($data['content']); $i++){

            $data['content'][$i]=htmlspecialchars_decode($data['content'][$i]);
            // 判断是否修改文章中图片的默认的alt 和title
            $image_title_alt_word=C('IMAGE_TITLE_ALT_WORD');
            if(!empty($image_title_alt_word)){
                // 修改图片默认的title和alt
                $data['content'][$i]=preg_replace('/title=\"(?<=").*?(?=")\"/','title="后台管理"',$data['content'][$i]);
                $data['content'][$i]=preg_replace('/alt=\"(?<=").*?(?=")\"/','alt="后台管理"',$data['content'][$i]);
            }
            // 将绝对路径转换为相对路径
            $data['content'][$i]=preg_replace('/src=\"^\/.*\/Upload\/image\/ueditor$/','src="/Upload/image/ueditor',$data['content'][$i]);
            // 转义
            $data['content'][$i]=htmlspecialchars($data['content'][$i]);
        }

        $data['content_facilities'] = $data['content'][0];
        $data['content_achievement'] = $data['content'][1];
        $data['content_spirit'] = $data['content'][2];
        $data['content_admin'] = $data['content'][3];
        //将学校相关处理
        $data['schoolassociation'] = implode(',', $data['category']);

        if($this->create($data)){
            //获取文章内容图片
            if($sid=$this->add()){
                for($j=0; $j<count($data['content']); $j++){
                    $image_path=get_ueditor_image_path($data['content'][$j]);
                    if(!empty($image_path)){
                        // 传递图片插入数据库
                        D('SchoolinfoPic')->addData($sid,$image_path);
                    }
                }

                return $sid;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }

    // 修改数据
    public function editData(){
        // 获取post数据
        $data=I('post.');
//        print_r($data);exit;
        // 反转义为下文的 preg_replace使用
        for($i=0; $i<count($data['content']); $i++){

            $data['content'][$i]=htmlspecialchars_decode($data['content'][$i]);
            // 判断是否修改文章中图片的默认的alt 和title
            $image_title_alt_word=C('IMAGE_TITLE_ALT_WORD');
            if(!empty($image_title_alt_word)){
                // 修改图片默认的title和alt
                $data['content'][$i]=preg_replace('/title=\"(?<=").*?(?=")\"/','title="后台管理"',$data['content'][$i]);
                $data['content'][$i]=preg_replace('/alt=\"(?<=").*?(?=")\"/','alt="后台管理"',$data['content'][$i]);
            }
            // 将绝对路径转换为相对路径
            $data['content'][$i]=preg_replace('/src=\"^\/.*\/Upload\/image\/ueditor$/','src="/Upload/image/ueditor',$data['content'][$i]);
            // 转义
            $data['content'][$i]=htmlspecialchars($data['content'][$i]);
        }

        $data['content_facilities'] = $data['content'][0];
        $data['content_achievement'] = $data['content'][1];
        $data['content_spirit'] = $data['content'][2];
        $data['content_admin'] = $data['content'][3];
        //将学校相关处理
        $data['schoolassociation'] = implode(',', $data['category']);

        if($this->create($data)){
            $sid=$data['sid'];
            $this->where(array('sid'=>$sid))->save();


            // 删除图片路径
            D('SchoolinfoPic')->deleteData($sid);
            for($j=0; $j<count($data['content']); $j++){
                $image_path=get_ueditor_image_path($data['content'][$j]);
                if(!empty($image_path)){
                    // 传递图片插入数据库
                    D('SchoolinfoPic')->addData($sid,$image_path);
                }
            }
            return true;
        }else{
            return false;
        }
    }

    // 彻底删除
    public function deleteData(){
        $sid=I('get.sid',0,'intval');
        D('SchoolinfoPic')->deleteData($sid);
        D('SchoolinfoPic')->deleteData($sid);
        $this->where(array('sid'=>$sid))->delete();
        return true;
    }

}
